package kalender;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

import Server.DBConnection;

public class Hovedklasse extends Thread {
	private static List<Person> personer;
	private static List<Rom> romliste;
	private static List<Avtale> avtaleliste;
	private static List<Melding> meldingsliste;
	private static DBConnection conn;
	private static Scanner skanner;
	private static Input input;
	private static EchoClient client;
	private static Person brukerInnlogget;
	
	public static void main(String[] args) throws Exception {
		init();
		opprettTester();
		brukerKommunikasjon();
		client.close();
	}
	
	private static void opprettTester() throws IOException{
		
	}
	
	public static void setMeldingsliste() throws IOException{
		meldingsliste.clear();
		meldingsliste = client.getMessageList();
	}
	
	public static ArrayList<Melding> getMeldingsliste(){
		return (ArrayList<Melding>) meldingsliste;
	}
	
	private static void init() throws Exception {
		personer = new ArrayList<Person>();
		romliste = new ArrayList<Rom>();
		client = new EchoClient("localhost", 8001);
		getAll();
	}
	
	public static Mote getMote(Person moteleder){
		for(Avtale mote : avtaleliste){
			if(mote.getOpprettetAv().getID() == moteleder.getID()){
				if(mote instanceof Mote){
					return (Mote)mote;
				}
			}
		}
		return null;
	}
	
	public static Mote getMote(int avtaleID){
		for(Avtale a : avtaleliste){
			if(a.getId() == avtaleID && (a instanceof Mote)){
				return (Mote) a;
			}
		}
		return null;
	}
	
	public static List<Rom> lagLedigeRomListe(String dato){
		List<Rom> ledigeRom = new ArrayList<Rom>();
		for(Rom rom :romliste){
			if(rom.isLedig(dato)){
				ledigeRom.add(rom);
			}
		}
		return null;
	}
		
	public static void getAll() throws IOException, NumberFormatException, ClassNotFoundException, SQLException {
		personer = client.getPersonList();
		
		for(Person p: personer){
			p.setEchoClient(client);
		}
		
		romliste = client.getRoomList();
		avtaleliste = client.getAppointmentList();
		meldingsliste = client.getMessageList();
		client.getOccupiedList();
	}
	
	public static int refreshAppointmentListAndGetNewID() throws NumberFormatException, IOException, ClassNotFoundException, SQLException {
		avtaleliste.clear();
		avtaleliste = client.getAppointmentList();
		return avtaleliste.get(avtaleliste.size()-1).getId()+1;
	}
	
	private static void brukerKommunikasjon() throws IOException, ClassNotFoundException, SQLException{
		input = new Input();
	}
	
	public static List<Person> getPersoner(){
		return personer;
	}
	
	public static void setBrukerInnlogget(Person p){
		brukerInnlogget = p;
	}
	public static Person getBrukerInnlogget(){
		return brukerInnlogget;
	}
	
	public static List<Rom> getRomliste(){
		return romliste;
	}
	
	public static EchoClient getClient(){
		return client;
	}
	
	public static Person getPersonWithID(int id){
		for (Person person : personer){
			if(person.getID() == id){
				return person;
			}
		}
		return null;
	}
	public static Rom getRomWithID(int id){
		for (Rom rom : romliste){
			if(rom.getRomID() == id){
				return rom;
			}
		}
		return null;
	}
	
	public static Avtale getAvtaleWithID(int id){
		for(Avtale a : avtaleliste){
			if(a.getId() == id)
				return a;
		}
		return null;
	}
}